<template>
  <el-dialog
    title="初始化"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="销售平台" prop="blonPlantform">
        <el-select
          v-model="dataForm.blonPlantform"
          clearable
          @change="pfChange"
          placeholder="请选择销售平台"
        >
          <el-option
            v-for="item in pfs"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="店铺" prop="store">
        <el-select v-model="dataForm.store" clearable placeholder="请选择店铺">
          <el-option
            v-for="item in stores"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="上下架删除" prop="status" v-if="type">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">上架</el-radio>
          <el-radio :label="-2">下架</el-radio>
          <el-radio :label="-1">删除</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      type: 0,
      dataForm: {
        dtId: null,
        blonPlantform: null,
        store: null,
        status: 0,
      },
      pfs: [],
      stores: [],
      groups: [],
      dtList: [],
      dtListTreeProps: {
        label: "name",
        children: "children",
      },
      dataRule: {
        blonPlantform: [
          { required: true, message: "请选择销售平台", trigger: "blur" },
        ],
        store: [{ required: true, message: "请选择店铺", trigger: "blur" }],
        status: [{ required: true, message: "请选择上下架", trigger: "blur" }],
      },
    };
  },
  methods: {
    // 获取机构及销售平台下拉列表数据
    getQuryPf() {
      this.$http({
        url: this.$http.adornUrl("/op/taskstatus/pf"),
        method: "get",
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.pfs = data.spfs;
        }
      });
    },
    // 查询店铺信息
    getStore() {
      this.$http({
        url: this.$http.adornUrl("/op/taskstatus/store"),
        method: "get",
        params: this.$http.adornParams({
          pf: this.dataForm.blonPlantform,
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.stores = data.list;
        }
      });
    },
    // 选择销售平台
    pfChange(value) {
      this.getStore();
    },
    init(type) {
      this.type = type;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        this.getQuryPf();
      });
    },
    // 表单提交
    dataFormSubmit() {
      //上下架
      if (this.type) {
        this.storeUpDown();
      } else {
        //初始化推送
        this.storeInt();
      }
    },
    // 店铺酒店推送
    storeInt() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$confirm("确定向店铺推送所有酒店吗?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              this.$http({
                url: this.$http.adornUrl("/op/taskstatus/init/store"),
                method: "get",
                params: this.$http.adornParams({
                  storId: this.dataForm.store,
                }),
              }).then(({ data }) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: "开始上架...",
                    type: "success",
                    duration: 1000,
                    onClose: () => {
                      this.visible = false;
                      this.$emit("refreshDataList");
                    },
                  });
                } else {
                  this.$message.error(data.msg);
                }
              });
            })
            .catch(() => {});
        }
      });
    },
    // 店铺酒店上下架
    storeUpDown() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$confirm("确定上下架所有酒店吗?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              this.$http({
                url: this.$http.adornUrl("/op/taskstatus/updown/store"),
                method: "get",
                params: this.$http.adornParams({
                  storId: this.dataForm.store,
                  status: this.dataForm.status,
                }),
              }).then(({ data }) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: "开始上架...",
                    type: "success",
                    duration: 1000,
                    onClose: () => {
                      this.visible = false;
                      this.$emit("refreshDataList");
                    },
                  });
                } else {
                  this.$message.error(data.msg);
                }
              });
            })
            .catch(() => {});
        }
      });
    },
  },
};
</script>
